//
//  XCSingerController.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCSingerController.h"
#import "DBSphereView.h"
#import "NetworkingHandlerOfBlock.h"
#import "XCModelForSInger.h"
#import "Masonry.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "XCSearchController.h"
#import "XCSingerDetailController.h"



#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667

@interface XCSingerController ()

@property (nonatomic, retain) NSMutableArray *arrayForSinger;
@property (nonatomic, retain) UITextField *fieldForSearch;
@property (nonatomic, retain) DBSphereView *shareView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, assign) NSInteger i;

@end

@implementation XCSingerController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.frame];
    image.image = [UIImage imageNamed:@"201564"];
    [self.view addSubview:image];

    [self handleData];
    
    
}

- (void)createSearch {

    self.fieldForSearch = [[UITextField alloc] init];
    self.shareView = [[DBSphereView alloc] init];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.fieldForSearch];
    [self.view addSubview:self.shareView];
    [self.view addSubview:self.button];
    
    self.fieldForSearch.placeholder = @"输入搜索内容";
    self.fieldForSearch.backgroundColor = [UIColor whiteColor];
    self.fieldForSearch.alpha = 0.6;
    self.fieldForSearch.layer.borderWidth = 1;
    self.fieldForSearch.layer.masksToBounds = YES;
    
    
    self.button.backgroundColor = [UIColor whiteColor];
    self.button.alpha = 0.6;
    self.button.layer.borderWidth = 1;
    self.button.layer.masksToBounds = YES;
    [self.button setTitle:@"搜索" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.fieldForSearch mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).equalTo(@64);
        make.left.equalTo(self.view).equalTo(@(10 * WIDGHTT));
        make.right.equalTo(self.button.mas_left).equalTo(@0);
        make.height.equalTo(self.fieldForSearch.mas_width).multipliedBy(1.0f / 9.0f);
        
    }];
    
    
    [self.shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).equalTo(@(5 * WIDGHTT));
        make.right.equalTo(self.view).equalTo(@(-5 * WIDGHTT));
        make.top.equalTo(self.fieldForSearch.mas_bottom).with.offset((60 * HEIGHTT));
        make.bottom.equalTo(self.view).equalTo(@-49);
        
    }];
    
   [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
       
       make.top.equalTo(self.view).equalTo(@64);
       make.left.equalTo(self.fieldForSearch.mas_right).equalTo(@(0));
       make.right.equalTo(self.view).equalTo(@(-10 * WIDGHTT));
       make.height.equalTo(self.fieldForSearch.mas_width).multipliedBy(1.0f / 9.0f);

       
       
   }];
 

    NSMutableArray *array = [NSMutableArray array];

    self.i = 0;
    for (int i = 0; i < 70; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, 0, 100 * WIDGHTT, 30 * WIDGHTT);
        
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [self.shareView addSubview:button];
        button.alpha = 0.8;
        button.tag = self.i;
       self.i++;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

        XCModelForSInger *model = [self.arrayForSinger objectAtIndex:i];
        
        [button setTitle:model.singer_name forState:UIControlStateNormal];
        
        [array addObject:button];
        
    }
    
   
    [self.shareView setCloudTags:array];
    

}


- (void)buttonPressed:(UIButton *)btn
{
    [self.shareView timerStop];
    
    XCSingerDetailController *detail = [[XCSingerDetailController alloc] init];
    
    [UIView animateWithDuration:0.3 animations:^{
        btn.transform = CGAffineTransformMakeScale(2., 2.);
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            
            btn.transform = CGAffineTransformMakeScale(1., 1.);
            
         XCModelForSInger *model = [self.arrayForSinger objectAtIndex:btn.tag];
            NSString *url = @"http://api.dongting.com/song/singer/";
            NSString *singerID = model.singer_id;
            NSString *urlBack = @"?detail=true&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88247&longtitude=121.5397";
          
            
            NSString *strUrlBack = @"/songs?page=1&size=50&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88245&longtitude=121.5396";
            
            
            NSString *singerURL = [url stringByAppendingFormat:@"%@%@", singerID,urlBack];
            
            NSString *singerCell = [url stringByAppendingFormat:@"%@%@", singerID, strUrlBack];
            
            detail.url = singerURL;
            detail.urlForCell = singerCell;
            
            
            [self.navigationController pushViewController:detail animated:YES];
            
        } completion:^(BOOL finished) {
            [self.shareView timerStart];
            
        }];
    }];
}

- (void)searchAction:(UIButton *) button {

    [self.view endEditing:YES];
    XCSearchController *search = [[XCSearchController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
    
    NSString *str1 = @"http://search.dongting.com/song/search?q=";
    
    NSString *str2 = [self.fieldForSearch.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString *str3 = @"&page=1&size=50&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=9.2&active=1&net=2&openudid=2928af1fecbb94c32045ea1742d7f5be6defe28e&idfa=87EE45D3-F1BC-49C6-AD99-E17773876586&utdid=Vu9EaFGgHV4DAPqTc15UrKj8&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=";
    
    search.url = [NSString stringWithFormat:@"%@%@%@",str1, str2,str3];
}

- (void)handleData {

NSString *url = @"http://api.dongting.com/misc/singer/top?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88247&longtitude=121.5397";
    
    [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        self.arrayForSinger = [NSMutableArray array];
        
        NSArray *array =[result objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
        
            XCModelForSInger *model = [[XCModelForSInger alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayForSinger addObject:model];
            
        }
        
        [self createSearch];
    }];



}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
